﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace OpenNETCF.Barcode
{
    public partial class BarcodePicture : PictureBox
    {
        private int m_ypos = -1;
        private Rectangle? m_rect = null;

        public BarcodePicture()
        {
            InitializeComponent();
        }

        public Rectangle? BarcodeBounds 
        {
            get { return m_rect; }
            set
            {
                m_rect = value;

                if ((value.HasValue) && (this.Image != null))
                {
                    var g = Graphics.FromImage(this.Image);
                    g.DrawRectangle(
                        new Pen(Color.Red, 2),
                        value.Value
                        );
                    Refresh();
                }
            }
        }

        public int YPos
        {
            get { return m_ypos; }
            set
            {
                if (m_ypos == value) return;

                m_ypos = value;
                Refresh();
            }
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
            
            if (this.Image == null) return;

            if ((YPos >= 0) && (YPos <= 100))
            {
                pe.Graphics.DrawLine(
                    new Pen(Color.LimeGreen),
                    new Point(0, (int)((YPos / 100F) * this.Height)),
                    new Point(this.Width, (int)((YPos / 100F) * this.Height))
                    );
            }
        }
    }
}
